﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisVenda.Web.messageBox;

namespace SisVenda.Web.Restrito
{
    public partial class CadastrarFornecedor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void odsFornecedor_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (tbxCle_Nome.Text != "")
                e.InputParameters["nome"] = tbxCle_Nome.Text;
            if (tbxFornec_Cnpj.Text != "")
                e.InputParameters["cnpj"] = tbxFornec_Cnpj.Text;
        }

        protected void gvwListagem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fvwEdicao.CurrentMode != FormViewMode.ReadOnly)
                fvwEdicao.ChangeMode(FormViewMode.ReadOnly);
            odsFormView.DataBind();
            fvwEdicao.DataBind();
        }

        protected void gvwListagem_DataBinding(object sender, EventArgs e)
        {
            gvwListagem.PageSize = Convert.ToInt16(ddlConsulta.SelectedValue);
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void ddlConsulta_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlUF_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((ObjectDataSource)fvwEdicao.FindControl("odsCidade")).DataBind();
        }

        protected void odsFormView_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void ddlUFA_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((ObjectDataSource)fvwEdicao.FindControl("odscidadeup")).DataBind();
        }

        protected void odsFormView_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void odsFormView_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (gvwListagem.SelectedDataKey != null)
            {
                e.InputParameters["idPessoa"] = gvwListagem.SelectedDataKey.Value;
            }
        }

        protected void odsFormView_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
           
            e.InputParameters["nome"] = ((TextBox)fvwEdicao.FindControl("tbxNome")).Text;
            e.InputParameters["cpf"] = "";
            e.InputParameters["rg"] = "";
            e.InputParameters["cnpj"] = ((TextBox)fvwEdicao.FindControl("tbxCNPJ")).Text;
            e.InputParameters["inscricaoEstadual"] = ((TextBox)fvwEdicao.FindControl("tbxIE")).Text;
            e.InputParameters["sexo"] = ((DropDownList)fvwEdicao.FindControl("ddlSexo")).SelectedValue;
            e.InputParameters["tipoPessoa"] = "F";
            e.InputParameters["ataNascimento"] = Convert.ToDateTime(((TextBox)fvwEdicao.FindControl("tbxDataNasci")).Text);
            e.InputParameters["estadoCivil"] = ((DropDownList)fvwEdicao.FindControl("ddlEstadoCivil")).SelectedValue;
            e.InputParameters["telefone"] = ((TextBox)fvwEdicao.FindControl("tbxTelefone")).Text;
            e.InputParameters["celular"] = ((TextBox)fvwEdicao.FindControl("tbxCeluar")).Text;
            e.InputParameters["fax"] = ((TextBox)fvwEdicao.FindControl("tbxFax")).Text;
            e.InputParameters["email"] = ((TextBox)fvwEdicao.FindControl("tbxEmail")).Text;
            e.InputParameters["descricao"] = ((TextBox)fvwEdicao.FindControl("tbxEndereco")).Text;
            e.InputParameters["bairro"] = ((TextBox)fvwEdicao.FindControl("tbxBairro")).Text;
            e.InputParameters["numero"] = ((TextBox)fvwEdicao.FindControl("tbxNumero")).Text;
            e.InputParameters["cep"] = ((TextBox)fvwEdicao.FindControl("tbxCep")).Text;
            e.InputParameters["idCidade"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlCidade")).SelectedValue);
            e.InputParameters["idCargo"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlCargo")).SelectedValue);
            e.InputParameters["idTipoTelefone"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlTipoTele")).SelectedValue);
            
            MessageBox.Show("Ação Efetuada com Sucesso!", MessageBox.MessageType.Success);
        }

        protected void odsFormView_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.InputParameters["idPessoa"] = Convert.ToInt32(((Label)fvwEdicao.FindControl("lblidup")).Text);
            e.InputParameters["idEndereco"] = Convert.ToInt32(((Label)fvwEdicao.FindControl("lblendidup")).Text);
            e.InputParameters["nome"] = ((TextBox)fvwEdicao.FindControl("tbxANome")).Text;
            e.InputParameters["cpf"] = "";
            e.InputParameters["rg"] = "";
            e.InputParameters["cnpj"] = ((TextBox)fvwEdicao.FindControl("tbxCNPJ")).Text;
            e.InputParameters["inscricaoEstadual"] = ((TextBox)fvwEdicao.FindControl("tbxARG")).Text;
            e.InputParameters["sexo"] = ((DropDownList)fvwEdicao.FindControl("ddlASexo")).SelectedValue;
            e.InputParameters["tipoPessoa"] = "F";
            e.InputParameters["dataNascimento"] = Convert.ToDateTime(((TextBox)fvwEdicao.FindControl("tbxADataNasci")).Text);
            e.InputParameters["estadoCivil"] = ((DropDownList)fvwEdicao.FindControl("ddlAEstadoCivil")).SelectedValue;
            e.InputParameters["telefone"] = ((TextBox)fvwEdicao.FindControl("tbxATelefone")).Text;
            e.InputParameters["celular"] = ((TextBox)fvwEdicao.FindControl("tbxACeluar")).Text;
            e.InputParameters["fax"] = ((TextBox)fvwEdicao.FindControl("tbxAFax")).Text;
            e.InputParameters["email"] = ((TextBox)fvwEdicao.FindControl("tbxAEmail")).Text;
            e.InputParameters["descricao"] = ((TextBox)fvwEdicao.FindControl("tbxAEndereco")).Text;
            e.InputParameters["bairro"] = ((TextBox)fvwEdicao.FindControl("tbxABairro")).Text;
            e.InputParameters["numero"] = ((TextBox)fvwEdicao.FindControl("tbxANumero")).Text;
            e.InputParameters["cep"] = ((TextBox)fvwEdicao.FindControl("tbxACep")).Text;
            e.InputParameters["idCidade"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlACidade")).SelectedValue);
            e.InputParameters["idCargo"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlACargo")).SelectedValue);
            e.InputParameters["idTipoTelefone"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlATipoTele")).SelectedValue);
        }
    }
}